#include "ReanimatedJSIUtils.h"
#include <vector>

using namespace facebook;

namespace reanimated::jsi_utils {

jsi::Array convertStringToArray(
    jsi::Runtime &rt,
    const std::string &value,
    const unsigned int expectedSize)
{
    std::vector<float> transformMatrixList;
    std::istringstream stringStream(value);
    std::copy(
        std::istream_iterator<float>(stringStream),
        std::istream_iterator<float>(),
        std::back_inserter(transformMatrixList));
    if (transformMatrixList.size() != expectedSize) {
        throw std::runtime_error("Transform matrix list size mismatch. Expected " + std::to_string(expectedSize) +
        ", got " + std::to_string(transformMatrixList.size()));
    }
    jsi::Array matrix(rt, expectedSize);
    for (unsigned int i = 0; i < expectedSize; i++) {
        matrix.setValueAtIndex(rt, i, transformMatrixList[i]);
    }
    return matrix;
}

} // namespace reanimated::jsi_utils
